//
//  UIViewController+OpenCamera.m
//  PlayVideo
//
//  Created by NoNoKing on 2016/12/26.
//  Copyright © 2016年 yyx. All rights reserved.
//

#import "UIViewController+OpenCamera.h"
#import "FFDefine.h"

NSString * const FFNotificKeyOpenCameraEditedImage = @"FFNotificKeyOpenCameraEditedImage";

NSString * const FFNotificNameOpenCameraChooseImg = @"FFNotificNameOpenCameraChooseImg";

@interface UIViewController()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation UIViewController (OpenCamera)

- (void)openCamera

{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"打开照相机");
        [self createImagePickerController:UIImagePickerControllerSourceTypeCamera];
    }else{
        NSLog(@"打不开相机");
    }

}

-(void)openPhotoLibrary

{
    [self createImagePickerController:UIImagePickerControllerSourceTypePhotoLibrary];
    
}

- (void)createImagePickerController:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
    // 进入相册
    [self presentViewController:picker animated:YES completion:^{
        
        FFLog(@"打开相册");
        
    }];
}

#pragma mark - imagepick 代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"%@",info);
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *editImg = [info objectForKey:UIImagePickerControllerEditedImage];
    NSMutableDictionary *notificInfo = [NSMutableDictionary dictionary];
    [notificInfo setObject:editImg forKey:FFNotificKeyOpenCameraEditedImage];
    [[NSNotificationCenter defaultCenter] postNotificationName:FFNotificNameOpenCameraChooseImg object:nil userInfo:notificInfo];
}


@end
